// const ffmpegPath = require('@ffmpeg-installer/ffmpeg')
// const ffprobePath = require('@ffprobe-installer/ffprobe')
const ffmpeg = require('fluent-ffmpeg')
//
//
// // 定义视频文件路径
// ffmpeg.setFfmpegPath(ffmpegPath.path)
// ffmpeg.setFfprobePath(ffprobePath.path)

async function getVideoInfo(video) {
    return new Promise((resolve, reject) => {
        ffmpeg.ffprobe(video, (err, data) => {
            resolve({
                'duration': data.format.duration,
                'durationF': formatSeconds(data.format.duration),
                'size': data.format.size,
                'sizeF': formatBytes(data.format.size),
            })
            // resolve(null)
        })
    })
}

async function main() {
    const value = await getVideoInfo('E:/other/dream_videos-local/抖音/如愿.mp4')
    console.log('Promise resolved with value:', value)
}

main()

function formatSeconds(seconds) {
    // 四舍五入取整
    seconds = Math.round(seconds)
    // 计算小时、分钟和秒
    const hrs = Math.floor(seconds / 3600)
    const mins = Math.floor((seconds % 3600) / 60)
    const secs = seconds % 60

    // 将小时、分钟和秒格式化为字符串
    const h = hrs.toString().padStart(2, '0')
    const m = mins.toString().padStart(2, '0')
    const s = secs.toString().padStart(2, '0')

    // 组合成最终的时间字符串
    return `${h}:${m}:${s}`
}

function formatBytes(bytes, decimals = 2) {
    if (bytes === 0) return '0'

    const k = 1024
    const sizes = ['B', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y']

    const i = Math.floor(Math.log(bytes) / Math.log(k))
    return (
        parseFloat((bytes / Math.pow(k, i)).toFixed(decimals)) + sizes[i]
    )
}
